package com.benedu.green.moon.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.benedu.cloud.common.core.base.result.R;
import com.benedu.cloud.common.core.base.result.ResObject;
import com.benedu.cloud.common.core.constant.ResCodeEnum;
import com.benedu.cloud.common.core.constant.SubResultCode;
import com.benedu.cloud.common.core.exception.BizException;
import com.benedu.cloud.common.mybatis.plus.util.BeanConvertUtils;
import com.benedu.cloud.common.redis.service.RedisService;
import com.benedu.green.moon.pojo.UserEntity;
import com.benedu.green.moon.pojo.dto.UserPageQueryParam;
import com.benedu.green.moon.repository.UserRepository;
import com.benedu.green.moon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:TODO
 * @author: xiaoguo
 * @date:2021/10/14 10:45
 * version:v1.0.0
 */
@RestController
public class IndexController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/getInfo")
    public ResObject getInfo(){
        int i = 1 / 0;
        return R.success();
    }


    @GetMapping("/list")
    public ResObject list(){
        throw new BizException(ResCodeEnum.ERROR, SubResultCode.SYSTEM_ERROR);
    }

    @PostMapping("/pageUser")
    public ResObject pageUser(@RequestBody UserEntity userPageQueryParam){
       /* UserEntity userEntity = new UserEntity();
        userEntity.setAccount("xiaoguo");
        userEntity.setPassword("xiaoguo");
        userEntity.setCreateTime(LocalDateTime.now());
        userEntity.setUpdateTime(LocalDate.now());
        redisService.set("userEntity::",userEntity);*/
        //dto -> entity
        //UserEntity userEntity = BeanConvertUtils.copy(userPageQueryParam,UserEntity.class);
        //IPage<UserEntity> pageList = userService.pageList(userEntity);
        IPage<UserEntity> userPage = userService.pageList(userPageQueryParam);
        redisService.set("userList", JSONObject.toJSONString(userPage.getRecords()));
        return R.success(userService.pageList(userPageQueryParam));
    }

    @GetMapping("/dbUserList")
    public ResObject dbUserList(){
        List<UserEntity> userEntityList = userService.list();
        return R.success(userEntityList);
    }
}
